package com.library.dcy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class MybatisUtil {
    static InputStream in;
    static SqlSession sqlSession;
    public static SqlSession getSession(String path)throws Exception{
        //1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream(path);
        //2.获取SqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.获取SqlSession对象
        sqlSession = factory.openSession(true);
        return sqlSession;
    }
    public static void close()throws Exception{
        sqlSession.close();
        in.close();
    }
}
